<!--
 * @Descripttion: 
 * @Author: zhanyi
 * @Date: 2021-10-20 16:41:39
 * @FilePath: \wczd-mobile\src\pages\my-info\myInfo.vue
-->
<template>
  <div class="myInfo">
    <secondTopPanel>
      <div slot='title'>切换用户</div>
    </secondTopPanel>
		<div class="myInfo-main">
			<div class="user-list" v-for="(item,index) in loginUser">
				<div class="login-user-left">
				  <div class="company-name">{{ item.defaultDeptCompanyName || item.companyName }}</div>
				  <div class="post-name">{{ item.postName }}</div>
				</div>
				<div v-if="item.clickState==='1'" class="login-user-right-red">当前</div>
				<div v-else class="login-user-right" @click="sureChangeUser(item)">切换</div>
			</div>
		</div>
  </div>
</template>

<script>
import secondTopPanel from '@/pages/component/top-panel/secondTopPanel.vue'
import { getUserProfile,selectUserListByPersonId } from "@/api/user";

export default {
  name: 'myInfo',
  components: { secondTopPanel },
  data () {
    return {
      loginUser: [],
      user: {},
    }
  },
  computed: {
  },
  mounted () {
    this._getUserProfile()
  },
  watch: {},
  methods: {
    // 查询登录人员信息
    _getUserProfile () {
      getUserProfile().then(res => {
        this.user = res.data
				this.changeUser()
      })
    },
		// 切换账号
		changeUser() {
		  selectUserListByPersonId({ personId: this.user.personId }).then(res => {
		    this.loginUser = []
		    for (let i = 0; i < res.rows.length; i++) {
		      if (res.rows[i].userId === this.user.userId) {
		        res.rows[i].clickState = '1'
						res.rows[i].text = res.rows[i].companyName+' '+res.rows[i].postName
		        this.loginUser.push(res.rows[i])
		      }
		    }
		    for (let i = 0; i < res.rows.length; i++) {
		      if (res.rows[i].userId !== this.user.userId) {
		        res.rows[i].clickState = '0'
						res.rows[i].text = res.rows[i].companyName+' '+res.rows[i].postName
		        this.loginUser.push(res.rows[i])
		      }
		    }
		  })
		},
		sureChangeUser(item){
			uni.showLoading({
				title: '账号切换中...'
			})
			this.$store
			  .dispatch('sureChangeUser', { userId: item.userId })
			  .then((res) => {
			    if (res.code === 200) {
							uni.reLaunch({
							  url: '/pages/my-info/myInfo'
							})
							uni.hideLoading()
			      }
			    })
			    .catch(() => {
						uni.hideLoading()
			  })
		}
  },
}
</script>

<style scoped lang="scss">
.myInfo{
	background: white;
  height: 100%;
	.myInfo-main{
    height: 100%;
    padding: 120upx 0 30px 0;
		
		.user-list{
			border-bottom: 1px solid #f1f1f1;
			display: flex;
			justify-content: space-between;
			padding: 10upx 30upx;
			
			.login-user-left{
				.company-name{
					font-size: 30upx;
					font-weight: 600;
				}
				.post-name{
					color: #7a8088;
				}
			}
			
			.login-user-right{
				color: #ff503e;
				cursor: pointer;
				display: flex;
				align-items: center;
				white-space: nowrap;
				
			}
			.login-user-right-red{
				color: #3e7fff;
				cursor: pointer;
				display: flex;
				align-items: center;
				white-space: nowrap;
			}
		}
	}
}
.header{
	border-bottom: 1px solid #f1f1f1;
}
</style>
